package barecodes;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sawior
 */
public class PassesIdCoder {
    
    public static String codeId(Integer id) {
        StringBuffer ss = new StringBuffer(id.toString());  
        int j = 0;
        while (ss.length() <4) {  
               ss.insert(0, '0');
        }  
        while (j <4) {
               ss.insert(0, randNumber()); 
               ss.insert(ss.length(), randNumber());
               j++;
        }  
        
        return ss.toString();
    }
    
    private static String randNumber() {
        Integer round = (int) Math.floor(Math.random()*10);
        if(round == 10) {
            randNumber();
        }
        return round.toString();
    }
    
    public static Integer uncodeId(String idText){
        StringBuffer ss = new StringBuffer(idText);
        String data = ss.substring(4, 8);
        try {
            return Integer.parseInt(data);
        }
        catch(Exception ex){
            Logger.getLogger(PassesIdCoder.class.getName()).log(Level.SEVERE, "[CarPark] Cannor parse scanned code to passes id.", ex);	
            return null;
        }
    }
    
}
